import { message } from 'antd';
import { history } from 'umi';
import { useState } from 'react';
import React, { useRef } from 'react';
import { PageContainer } from '@ant-design/pro-layout';
import type { ProColumns, ActionType } from '@ant-design/pro-table';
import ProTable from '@ant-design/pro-table';
import { authlist, updateSort } from './service';
import { ModalForm, ProFormDigit } from '@ant-design/pro-form';
import type { TableListItem, TableListPagination } from './data';

const TableList: React.FC = () => {
  const actionRef = useRef<ActionType>();
  const [currentRow, setCurrentRow] = useState<Object>({});
  const [modalVisible, handleModalVisible] = useState<boolean>(false);
  const [formData, setFormData] = useState<API.RuleListItem>({});

  const handleSort = async (record: API.RuleListItem[]) => {
    const hide = message.loading('正在处理');
    try {
      const response = await updateSort(record);
      if (response.code == 200) {
        message.success('处理成功,即将刷新');
        actionRef.current ?.reloadAndRest ?.();
      } else {
        message.error('处理失败!');
      }
      hide();
      return true;
    } catch (error) {
      hide();
      message.error('处理失败');
      return false;
    }
  };

  const handle = async (record: API.RuleListItem[]) => {
    const hide = message.loading('正在处理');
    let item = { id: record.id };
    item.status = record.status == 1 ? 0 : 1;
    try {
      const response = await updateStatus(item);
      if (response.code == 200) {
        message.success('处理成功,即将刷新');
      } else {
        message.error('处理失败!');
      }
      hide();
      actionRef.current ?.reloadAndRest ?.();
    } catch (error) {
      hide();
      message.error('处理失败');
      return false;
    }
  };

  const columns: ProColumns<TableListItem>[] = [
    {
      title: '医院',
      dataIndex: 'name',
      hideInForm: true,
      hideInSearch: true,
    },
    {
      title: '认证状态',
      dataIndex: 'status',
      valueType: 'select',
      valueEnum: {
        0: { text: '待审核' },
        1: {
          text: '审核通过',
        },
        2: {
          text: '审核驳回',
        },
        3: {
          text: '终止审核',
        },
      },
      initialValue: '0'
    },
    {
      title: '入驻时间',
      dataIndex: 'createTime',
      hideInSearch: true,
    },
    {
      title: '排序',
      dataIndex: 'sort',
      hideInForm: true,
      hideInSearch: true,
    },
    // {
    //   title: '申请认证时间',
    //   dataIndex: 'createTime',
    //   width: '150px',
    //   valueType: 'date',
    // },
    // {
    //   title: '审核不通过原因',
    //   dataIndex: 'failReason',
    //   width: '350px',
    //   hideInForm: true,
    //   hideInSearch: true,
    // },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      width: '150px',
      render: (_, record) => [
        record.status == 0 ? (
          <a
            key="eidt"
            onClick={() => {
              history.push('/hospital/auth-detail?id=' + record.id);
            }}
          >
            审核
          </a>
        ) : (
            <a
              key="eidt"
              onClick={() => {
                history.push('/hospital/auth-detail?id=' + record.id);
              }}
            >
              查看
          </a>
          ),
        <a
          key="update"
          onClick={(e) => {
            e.preventDefault();
            setCurrentRow(record);
            handleModalVisible(true);
          }}
        >
          修改排序
              </a>
      ],
    },
  ];

  return (
    <PageContainer>
      <ProTable<TableListItem, TableListPagination>
        actionRef={actionRef}
      search={false}
      rowKey="id"
        search={{
        labelWidth: 120,
      }}
        // headerTitle={
        //   <Button
        //     type="primary"
        //     key="primary"
        //     onClick={() => {
        //       history.push('/doctor/doctor-detail');
        //     }}
        //   >
        //     <PlusOutlined /> 新增
        //   </Button>
        // }
        request = {
          async(
            // 第一个参数 params 查询表单和 params 参数的结合
            // 第一个参数中一定会有 pageSize 和  current ，这两个参数是 antd 的规范
            params,
            sorter,
            filter,
          ) => {
        console.log(params, formData, filter);
      // 这里需要返回一个 Promise,在返回之前你可以进行数据转化
      // 如果需要转化参数可以在这里进行修改
      let query = {
        likeQuery: {},
            eqQuery: {},
            pageNum: params.current,
            pageSize: params.pageSize,
          };
          delete params.current;
          delete params.pageSize;
          if (params.name == '') delete params.name;
          query.eqQuery = params;
          const msg = await authlist(query);
          if (msg.code == 200) {
            return {
        data: msg.data.list,
              // success 请返回 true，
              // 不然 table 会停止解析数据，即使有数据
              success: msg.message,
              // 不传会使用 data 的长度，如果是分页一定要传
              total: msg.data.total,
            };
          } else {
      }
      }}
        columns={columns}
      />
       {modalVisible ? (
        <ModalForm
          title={'更新排序'}
          width="500px"
          layout="horizontal"
          visible={modalVisible}
          onVisibleChange={handleModalVisible}
          onFinish={async (value) => {
            // value.viewNum = type;
            // value.likeNum = type;
            value.id = currentRow.id;
            console.log('value', value);
            let success = await handleSort(value as Object);
            if (success) {
              handleModalVisible(false);
            }
          }}
        >
          <ProFormDigit
            rules={[
              {
                required: true,
                message: '请输入',
              },
            ]}
            label="排序"
            width="md"
            min={1}
            name="sort"
          />
        </ModalForm>
      ) : null}

    </PageContainer>
  );
};

export default TableList;
